<?php

namespace App\Http\Controllers\Admin\Logs;

use App\Http\Controllers\AdminController;
use App\Models\Logs\ActivityLog;
use Illuminate\Http\Request;

class ActivityController extends AdminController
{

    public function data(Request $request)
    {
        $model = ActivityLog::query()->select(['id', 'log_name', 'description', 'subject_type', 'causer_id', 'properties', 'created_at']);
        if ($keyword = $request->get('keywords')) {
            $keyword = filterStr($keyword);
            $model->where(function ($query) use ($keyword) {
                $query->where('description', 'like', $keyword.'%')->orWhere('subject_type', 'like', $keyword.'%');
            });
        }
        if ($username = $request->get('username')) {
            $username = filterStr($username);
            $model->whereHas('user', function ($query) use ($username) {
                $query->where('username', 'like', filterStr($username).'%');
            });
        }
        $list = $model->with('user:id,username')->orderByDesc('id')->paginate($request->get('limit', 10));
        return $this->succeed([
            'total' => $list->total(),
            'rows' => $list->items()
        ]);
    }
}
